package top.lyjwn.todo.redis.impl;

import org.noear.redisx.RedisClient;
import org.noear.redisx.RedisSession;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import top.lyjwn.todo.redis.RedisMap;

import java.util.Arrays;

@Component
public class RedisMapJSONObjectImpl implements RedisMap {

    @Inject(required = false)
    private RedisClient redisClient;

    @Inject("${todo.redis.expire}")
    private Integer expire = 120;

    @Override
    public boolean IsRedisConfig() {
        return redisClient != null;
    }

    @Override
    public boolean expire(String key, Integer time) {
        try {
            redisClient.open(session -> {
                session.key(key).expire(time);
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



    @Override
    public long getExpire(String key) {
        try {
            RedisSession redisSession = redisClient.openSession();
            return redisSession.ttl();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public boolean delay(String key,int seconds) {
        try {
            redisClient.open(session -> {
                session.key(key).delay(seconds);
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hasKey(String key) {
        try {
            redisClient.open(session -> {
                session.key(key).exists();
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void delete(String... key) {
        try {
            redisClient.open(session -> {
                session.deleteKeys(Arrays.stream(key).toList());
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean set(String key, String value) {
        try {
            redisClient.open(session -> {
                session.key(key).expire(expire).set(value);
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean set(String key, String value, Integer time) {
        try {
            redisClient.open(session -> {
                session.key(key).expire(time).set(value);
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String get(String key) {
        try {
            String tmp = redisClient.openAndGet(session -> session.key(key).get());
            return tmp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
